package ar.com.pragmatia.lanacion;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import ar.com.pragmatia.lanacion.app.App;
import ar.com.pragmatia.lanacion.data.Syncronizer;

public class LoginActivity extends Activity {

	EditText txtUsername;
	EditText txtPassword;
	Intent parquimetroActivityIntent;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		App.initializate(this);

		if (!App.getUser().isEmpty()) {

			Intent myIntent = new Intent().setClass(this,
					MainClientActivity.class);
			startActivity(myIntent);
			finish();
		}
		txtUsername = (EditText) findViewById(R.id.txtUsername);
		txtPassword = (EditText) findViewById(R.id.txtPassword);

		parquimetroActivityIntent = new Intent(this, MainClientActivity.class);

		OnClickListener login = new OnClickListener() {

			public void onClick(View v) {
				new doLogin().execute();

				// String text = "";
				//
				// String username = txtUsername.getText().toString();
				// String password = txtPassword.getText().toString();
				//
				// if (username.equals(getDbUsername())
				// && password.equals(getDbPassword())) {
				//
				// startActivity(parquimetroActivityIntent);
				// } else {
				// text = getText(R.string.login_error).toString();
				// int duration = Toast.LENGTH_SHORT * 2;
				// Toast toast = Toast.makeText(getApplicationContext(), text,
				// duration);
				// toast.show();
				// }
			}
		};

		Button btnLogin = (Button) findViewById(R.id.btnLogin);
		btnLogin.setOnClickListener(login);

		// PendingClient client = new PendingClient();
		// Vector<AppData> vec;
		// vec = client.find();
		// App.log("LoginActivity", "onPostExecute",
		// new Integer(vec.size()).toString());

	}

	public String getDbUsername() {
		return "dribba";
	}

	public String getDbPassword() {
		return "hunter";
	}

	private class doLogin extends AsyncTask<Void, Void, Void> {

		ProgressDialog progressDialog = null;
		private String username;
		private String password;
		boolean logedIn = false;

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);

			progressDialog.dismiss();

			if (logedIn) {
				startActivity(parquimetroActivityIntent);
			} else {
				String text = getText(R.string.login_error).toString();
				int duration = Toast.LENGTH_SHORT * 2;
				Toast toast = Toast.makeText(getApplicationContext(), text,
						duration);
				toast.show();
			}

		}

		@Override
		protected Void doInBackground(Void... params) {
			logedIn = false;

			username = txtUsername.getText().toString();
			password = txtPassword.getText().toString();

			logedIn = (Syncronizer.logIn(username, password) && !App.getUser()
					.isEmpty());
			return null;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			username = "";
			password = "";
			progressDialog = ProgressDialog.show(LoginActivity.this,
					getString(R.string.wait), getString(R.string.loading),
					true, false);
		}

	}

}